package com.qf.agro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.common.SysUserHolder;
import com.qf.agro.mapper.DeviceStaticMapper;
import com.qf.agro.pojo.SmartDeviceDataLogs;
import com.qf.agro.pojo.SmartDeviceLog;
import com.qf.agro.pojo.SmartDeviceStatic;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.DeviceStaticService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.service.impl
 * @Date 2023/7/5 20:56
 */
@Service
public class DeviceStaticServiceImpl implements DeviceStaticService {

    @Autowired
    private DeviceStaticMapper deviceStaticMapper;
    @Override
    public ResultInfo getPage(Integer pageNum, Integer pageSize, String name) {


        Page<SmartDeviceLog> page=new Page<>(pageNum,pageSize);
        SysUser sysUser = SysUserHolder.get();
        LambdaQueryWrapper<SmartDeviceLog> lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null,SmartDeviceLog::getDeviceName,name);
        if(sysUser!=null){
            lqw.eq(SmartDeviceLog::getCompanyId,sysUser.getCompanyId());
        }
       page= deviceStaticMapper.selectPage(page,lqw);


        System.out.println(page);
        return ResultInfo.success(page);
    }

    @Override
    public ResultInfo getCount() {
        Date date=new Date();
        Date addDays = DateUtils.addDays(date, -1);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String after = simpleDateFormat.format(date);
        String before = simpleDateFormat.format(addDays);
        SysUser sysUser = SysUserHolder.get();
        Long companyId = sysUser.getCompanyId();

        List<SmartDeviceLog> list=  deviceStaticMapper.getCount(before,after,companyId);
        return ResultInfo.success(list);
    }
}
